import 'dart:math';

import 'package:cbec_app/components/base/base_icon.dart';
import 'package:cbec_app/utils/ThemeConfig.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

String _defaultText = "未设置滚动文字!!!";

///店铺装修-公告
class DecorationNotice extends StatefulWidget {
  ///公告配置
  final Map<String, dynamic> info;

  const DecorationNotice({
    super.key,
    required this.info,
  });

  @override
  State<DecorationNotice> createState() => _DecorationNoticeState();
}

class _DecorationNoticeState extends State<DecorationNotice> {
  @override
  Widget build(BuildContext context) {
    return getBox();
  }

  ///获取容器
  Widget getBox() {
    switch (widget.info['card_vertical_padding']) {
      case 0: //无边
        return Container(color: Tools.fromHex(widget.info['card_bg_color']), child: noticeBox());
      case 1: //有边
      default:
        return Container(
          color: Tools.fromHex(widget.info['card_bg_color']),
          child: noticeBox(),
        );
    }
  }

  ///公告容器
  Widget noticeBox() {
    return Container(
      decoration: BoxDecoration(
        color: Tools.fromHex(widget.info['bg_color']),
        borderRadius: BorderRadius.all(
          Radius.circular(widget.info['card_border_radius'] == 0 ? 5 : 0),
        ),
      ),
      margin: EdgeInsets.only(left: getMargin(), right: getMargin(), top: 5, bottom: 5),
      padding: const EdgeInsets.only(top: 10, bottom: 10),
      child: Row(
        children: [
          Padding(
            padding: const EdgeInsets.only(left: 10),
            child: BaseIcon(
              url: 'horn.png',
              color: Tools.fromHex('#ec544d'),
              size: 14,
            ),
          ),
          Container(
            width: getScrollWidth(),
            margin: const EdgeInsets.only(left: 10),
            child: getTextWidth() >= getScrollWidth()
                ? widget.info['content']
                : Text(
                    widget.info['content'],
                    style: TextStyle(fontSize: 14, color: Tools.fromHex(widget.info['color'])),
                  ),
          )
        ],
      ),
    );
  }

  ///获取页面间距
  double getMargin() {
    return double.parse(widget.info['page_margin'].toString());
  }

  ///获取滚动宽度
  double getScrollWidth() {
    return 375.w - getMargin() * 2 - 50;
  }

  ///获取文本宽度
  double getTextWidth() {
    return 14.0 * (widget.info['content'] as String).length;
  }
}
